package com.centling.smartSealForPhone.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.application.SmartSeal;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.wsdk.service.BluetoothLeService;
import com.fionera.base.util.MD5Util;
import com.centling.smartSealForPhone.utils.OkHttpUtil;
import com.centling.smartSealForPhone.utils.ShowToast;

import java.util.HashMap;
import java.util.Map;

public class RegisterSubmitActivity extends TitleBarActivity implements View.OnClickListener {

    private EditText registerZhangName;
    private TextView registerSn;
    private EditText registerAdmin;
    private EditText registerRealName;
    private EditText registerAdminPassword;
    private EditText registerAdminPasswordR;
    private EditText registerChecker;
    private EditText registerCheckerPassword;
    private EditText registerCheckerPasswordR;
    private EditText registerOrg;
    private EditText registerCheckerRealName;
    private CheckBox checkBox;
    private String zName;
    private String mSN;
    private String adminNum;
    private String adminPassword;
    private String adminPasswordR;
    private String checkerName;
    private String realName;
    private String checkerPassword;
    private String checkerPasswordR;
    private String orgName;
    private String checkerRealName;

    public static String ascToString(String ascValue) {

        int len = ascValue.length();
        StringBuilder value = new StringBuilder();
        for (int i = 0; i < len; i++, i++) {
            String asc = ascValue.substring(i, i + 2);
            value.append((char) Integer.parseInt(asc, 16));
        }

        return value.toString();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg_submit);

        setTitleBarText("云注册");

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        registerZhangName = (EditText) findViewById(R.id.register_zhang_name);
        registerSn = (TextView) findViewById(R.id.register_sn);
        registerRealName = (EditText) findViewById(R.id.register_name);
        registerAdmin = (EditText) findViewById(R.id.register_admin);
        registerAdminPassword = (EditText) findViewById(R.id.register_admin_password);
        registerAdminPasswordR = (EditText) findViewById(R.id.register_admin_passwordR);
        registerChecker = (EditText) findViewById(R.id.register_checker);
        registerCheckerPassword = (EditText) findViewById(R.id.register_checker_password);
        registerCheckerPasswordR = (EditText) findViewById(R.id.register_checker_passwordR);
        registerCheckerRealName = (EditText) findViewById(R.id.register_auditor_name);
        registerOrg = (EditText) findViewById(R.id.register_org);
        checkBox = (CheckBox) findViewById(R.id.cb_register_pro);

        TextView registerPro = (TextView) findViewById(R.id.tv_register_pro);
        TextView registerFinish = (TextView) findViewById(R.id.register_finish);
        registerFinish.setOnClickListener(this);
        Spannable span = new SpannableString(registerPro.getText());
        span.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.blue1)), 8,
                12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        registerPro.setText(span);
        registerPro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(RegisterSubmitActivity.this, UserProtocolActivity.class));
            }
        });
        initData();
    }

    private void initData() {
        zName = registerZhangName.getText().toString().trim();
        mSN = getIntent().getStringExtra("SN");
        registerSn.setText(ascToString(mSN));
        realName = registerRealName.getText().toString().trim();
        adminNum = registerAdmin.getText().toString().trim();
        adminPassword = registerAdminPassword.getText().toString().trim();
        adminPasswordR = registerAdminPasswordR.getText().toString().trim();
        checkerName = registerChecker.getText().toString().trim();
        checkerRealName=registerCheckerRealName.getText().toString().trim();
        checkerPassword = registerCheckerPassword.getText().toString().trim();
        checkerPasswordR = registerCheckerPasswordR.getText().toString().trim();
        orgName = registerOrg.getText().toString().trim();
    }

    @Override
    public void onClick(View view) {
        initData();

        if (TextUtils.isEmpty(zName)) {
            ShowToast.shortTime("请输入印章名称");
            return;
        }

        if (TextUtils.isEmpty(orgName)) {
            ShowToast.shortTime("请输入机构名称");
            return;
        }

        if (TextUtils.isEmpty(realName)) {
            ShowToast.shortTime("请输入管理员姓名");
            return;
        }
        if (TextUtils.isEmpty(adminNum)) {

            ShowToast.shortTime("请输入管理员账号");
            return;
        }
//        if (adminNum.length() < 6) {
//            ShowToast.shortTime("请输入6~20位管理员账号");
//            return;
//        }

        if (TextUtils.isEmpty(adminPassword)) {

            ShowToast.shortTime("请输入管理员密码");
            return;
        }
        if (adminPassword.length() < 6) {
            ShowToast.shortTime("请输入6~20位管理员密码");
            return;
        }
        if (TextUtils.isEmpty(adminPasswordR)) {
            ShowToast.shortTime("请确认管理员密码");
            return;
        }
        if (!adminPasswordR.equals(adminPassword)) {
            ShowToast.shortTime("两次输入管理员密码不一致");
            return;
        }

        if (TextUtils.isEmpty(checkerRealName)) {
            ShowToast.shortTime("请输入审核员姓名");
            return;
        }
        if (TextUtils.isEmpty(checkerName)) {
            ShowToast.shortTime("请输入审核员账号");
            return;
        }
//        if (checkerName.length() < 6) {
//            ShowToast.shortTime("请输入6~20位审核员账号");
//            return;
//        }

        if (TextUtils.isEmpty(checkerPassword)) {
            ShowToast.shortTime("请输入审核员密码");
            return;
        }
        if (checkerPassword.length() < 6) {
            ShowToast.shortTime("请输入6~20位审核员密码");
            return;
        }
        if (TextUtils.isEmpty(checkerPasswordR)) {

            ShowToast.shortTime("请输入审核员密码");
            return;
        }
        if (!checkerPasswordR.equals(checkerPassword)) {
            ShowToast.shortTime("两次输入审核员员密码不一致");
            return;
        }
        if (adminNum.equals(checkerName)) {
            ShowToast.shortTime("管理员账号和审核员账号不能重复");
            return;
        }
        if (!checkBox.isChecked()) {
            ShowToast.shortTime("请接受用户协议");
            return;
        }

        Map<String, String> param = new HashMap<>();
        param.put("sn", mSN);
        param.put("sealName", zName);
        param.put("orgName", orgName);
        param.put("clientAdminName", adminNum);
        param.put("clientAdminRealName", realName);
        param.put("clientAdminPassword", MD5Util.md5(adminPassword));
        param.put("serverAdminName", checkerName);
        param.put("serverAdminRealName",checkerRealName);
        param.put("serverAdminPassword", MD5Util.md5(checkerPassword));
        param.put("companyCode", SmartSeal.getInstance().getIp());
        putJsonEnqueue(UrlConstants.ADD_EQUIP, param, new OkHttpUtil.NetCallBack() {
            @Override
            public void onSucceed(String json) {
                ShowToast.shortTime("设备注册成功");
                finish();
            }

            @Override
            public void onFailed(String reason) {
                ShowToast.shortTime(reason);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BluetoothLeService.disconnect();
    }
}
